package com.ruoyi.web.controller.wechatApplet;

import com.ruoyi.background.domain.AppSite;
import com.ruoyi.background.domain.AppSiteTopic;
import com.ruoyi.background.domain.AppSiteTopicUser;
import com.ruoyi.background.domain.AppSiteTopicUserDetail;
import com.ruoyi.background.service.IAppSiteService;
import com.ruoyi.background.service.IAppSiteTopicService;
import com.ruoyi.background.service.IAppSiteTopicUserDetailService;
import com.ruoyi.background.service.IAppSiteTopicUserService;
import com.ruoyi.background.utils.SYS_CONSTANT;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/wechat/site")
public class SiteController extends BaseController {
    @Autowired
    private IAppSiteService appSiteService;

    @Autowired
    private IAppSiteTopicService siteTopicService;

    @Autowired
    private IAppSiteTopicUserService topicUserService;

    @Autowired
    private IAppSiteTopicUserDetailService detailService;

    @Autowired
    private RedisCache redisCache;

    /**
     * 获取关卡列表
     *
     * @param appSite 获取关卡列表
     * @return list
     */
    @GetMapping("/getList")
    public TableDataInfo getSiteList(AppSite appSite) {
//        String key = REDIS_KEY.SITE_LIST_KEY;
//        List<AppSite> siteList = redisCache.getCacheList(key);
//        if (ObjectUtils.isEmpty(siteList)) {
//            appSite.setStatus(0);
//            siteList = appSiteService.selectAppSiteList(appSite);
//            redisCache.deleteByKey(key);
//            redisCache.setCacheList(key, siteList);
//            redisCache.expire(key, 2 * 60 * 7, TimeUnit.MINUTES);
//        }
//        return getDataTable(siteList);
        return getDataTable(SYS_CONSTANT.APP_SITE_LIST_CONS);
    }

    /**
     * 通过id查找
     *
     * @param id id
     * @return 结果
     */
    @GetMapping("/get/{id}")
    public AjaxResult getInfoById(@PathVariable Long id) {
        AppSite site = appSiteService.selectAppSiteById(id);
        if (1 == site.getDelFlag()) return AjaxResult.error("活动关卡已删除！");
        if (1 == site.getStatus()) return AjaxResult.error("活动关卡已下架");
        return AjaxResult.success(site);
    }

    /**
     * 根据id查询题目
     *
     * @param id 点位的id
     * @return 结果
     */
    @GetMapping("/getSiteTopic/{id}")
    public AjaxResult getSiteTopic(@PathVariable Long id) {
        return AjaxResult.success(siteTopicService.selectTopicBySiteId(id));
    }

    /**
     * 用户答题
     *
     * @param topic 题目
     * @return 结果
     */
    @PostMapping("/userAddTopic")
    public AjaxResult userAddTopic(@RequestBody List<AppSiteTopic> topic) {
        Map<String, String> map = siteTopicService.userAddTopic(topic);
        return AjaxResult.success(map);
    }

    /**
     * 获取用户的答题记录
     *
     * @param topicUser 查询
     * @return 结果
     */
    @GetMapping("/getUserTopicList")
    public TableDataInfo getUserTopic(AppSiteTopicUser topicUser) {
        startPage();
        topicUser.setUserId(SecurityUtils.getUserId());
        List<AppSiteTopicUser> topicList = topicUserService.selectAppSiteTopicUserList(topicUser);
        return getDataTable(topicList);
    }

    /**
     * 查询答题详情
     *
     * @param detail 查询条件
     * @return 结果
     */
    @GetMapping("/getUserTopicDetail")
    public TableDataInfo getUserTopicDetail(AppSiteTopicUserDetail detail) {
        startPage();
        List<AppSiteTopicUserDetail> detailList = detailService.selectAppSiteTopicUserDetailList(detail);
        for (AppSiteTopicUserDetail appSiteTopicUserDetail : detailList) {
            appSiteTopicUserDetail.initData();
        }
        return getDataTable(detailList);
    }

}
